package in.unlocker.apps.unlock.v1.rs;

import java.util.List;

import in.unlocker.apps.unlock.model.registration.User;
import in.unlocker.apps.unlock.model.ws.ParameterBean;
import in.unlocker.apps.unlock.service.UserService;
import in.unlocker.apps.unlock.service.exception.NotFoundException;
import in.unlocker.apps.unlock.v1.helper.DataMapperHelper;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.POST;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.Path;
import javax.ws.rs.BeanParam;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component
@Scope("prototype")
@Path("/user-management")
public class UserResource
{
	
	private User user;
	
	@Autowired
//	@Qualifier("userServiceImpl")
	private UserService  userService;

	@DELETE
//	@Path("/user/{id}")
	@Produces(MediaType.APPLICATION_JSON)
	public Response deleteUser(@BeanParam ParameterBean paramBean)
	{
		
		User user = DataMapperHelper.mapToUser(paramBean);
		if (!userService.deleteUser(user))
		{
			throw new NotFoundException("User was not found.");
		}
		return Response.noContent().build();
	}

	@GET
//	@Path("/user/{id}")
	@Path("/user")
	@Produces(MediaType.APPLICATION_JSON)
	public User getUser(@BeanParam ParameterBean paramBean)
	{
		User user1 = DataMapperHelper.mapToUser(paramBean);
		final User user = userService.getUser(user1);
		if (user == null)
		{
			throw new NotFoundException("User was not found.");
		}
		return DataMapperHelper.mapFromModel(user);
	}

	@GET
	@Path("{emailId}")
	public User finduserByEmail(@PathParam("emailId") final String emailId)
	{
		
		final User user = userService.getUserByEmail(emailId);
		if (user == null)
		{
			throw new NotFoundException("User was not found.");
		}
		return DataMapperHelper.mapFromModel(user);
	}
	
	@GET
	@Path("/query-params/")
	@Produces(MediaType.APPLICATION_JSON)
	public User getUserByEmailId(@QueryParam("emailId") String emailId)
	{
		final User user = userService.getUserByEmail(emailId);
		if (user == null)
		{
			throw new NotFoundException("User was not found.");
		}
		return DataMapperHelper.mapFromModel(user);
	}
	

	@GET
	@Path("/query-params1/")
	@Produces(MediaType.APPLICATION_JSON)
	public User getUserByEmailId1(@QueryParam(value = "item") final List<String> item)
	{
		final User user = userService.getUserByEmail(null);
		if (user == null)
		{
			throw new NotFoundException("User was not found.");
		}
		return DataMapperHelper.mapFromModel(user);
	}




	@Consumes(MediaType.APPLICATION_JSON)
	@PUT
	public Response updateUser(final User user)
	{
		userService.updateUser(user);
		return Response.noContent().build();
	}
	
	
	@Consumes(MediaType.APPLICATION_JSON)
	@POST
	public Response createUser(final User user)
	{
		userService.createUser(user);
		return Response.noContent().build();
	}

	
}
